<?php

namespace app\common\model;

use think\Model;

class Community extends Model
{
    // 表名
    protected $name = 'user';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        /*'scale_text',*/
        'setup_time_text',
        'is_pay_text',
        'is_company_text',
        'heldsdata_text',
       /* 'state_text'*/
    ];
    
    public function user()
    {
        return $this->belongsToMany('User','\app\api\model\CommunityMember');
    }

    public function getUserInfo($token)
    {
        $user_id = Cache::get(config('tokenPrefix') . $token);

        if ($user_id == false) return false;

        $user = $this->where('id',$user_id)->find();
        
        return $user;
    }

    
    public function getScaleList()
    {
        return ['1' => '0-50人','2' => '50-100人','3' => '100-300人','4' => '300-500人','5' => '500-1000人','6' => '1000人以上'];
    }     

    public function getIsPayList()
    {
        return ['0' => __('Is_pay 0'),'1' => __('Is_pay 1')];
    }     

    public function getIsCompanyList()
    {
        return ['0' => __('Is_company 0'),'1' => __('Is_company 1')];
    }     

    public function getHeldsdataList()
    {
        return model('ActivityCat')->column('id,name');
    }     

    public function getStateList()
    {
        return ['0' => __('State 0'),'1' => __('State 1'),'2' => __('State 2')];
    }     


    /*public function getScaleTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['scale'];
        $list = $this->getScaleList();
        return isset($list[$value]) ? $list[$value] : '';
    }*/

    public function getScaleAttr($value, $data)
    {        
        $value = $value ? $value : $data['scale'];
        $list = $this->getScaleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSetupTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['setup_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsPayTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['is_pay'];
        $list = $this->getIsPayList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsCompanyTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['is_company'];
        $list = $this->getIsCompanyList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHeldsdataTextAttr($value, $data)
    {
        $value = $value ? $value : $data['heldsdata'];
        $valueArr = explode(',', $value);
        $list = $this->getHeldsdataList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getStateAttr($value, $data)
    {        
        $value = $value ? $value : $data['state'];
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setSetupTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setHeldsdataAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


}
